package com.jason.location;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.os.Vibrator;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.jason.location.listener.MyLocationListener;

/**
 * @ClassName: BootApp
 * @Description: 全局应用程序类：用于保存和调用全局应用配置及访问网络数据
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年11月23日10:54:12
 */
public class BootApp extends Application {

    public static BootApp mInstance = null;
    /** 全局Context*/
    public static Context mAppContext;

    // Baidu LBS
    public LocationClient mLocationClient;
    public GeofenceClient mGeofenceClient;
    public MyLocationListener mMyLocationListener;
    public Vibrator mVibrator;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        mAppContext = getApplicationContext();
        initBaiduLBS();
    }


    private void initBaiduLBS() {
        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener(mAppContext);
        mLocationClient.registerLocationListener(mMyLocationListener);
        mGeofenceClient = new GeofenceClient(mAppContext);
        mVibrator = (Vibrator) getApplicationContext().getSystemService(
                Service.VIBRATOR_SERVICE);
        initLocation();
    }

    // 初始化定位
    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true);
        option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
        mLocationClient.setLocOption(option);
    }

    @Override
    // 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
    public void onTerminate() {
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
        super.onTerminate();
    }

    public static BootApp getInstance() {
        return mInstance;
    }

}
